Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@webcomponents/shadycss

Package Overview
Dependencies
Maintainers
4
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@webcomponents/shadycss

Polyfill for Scoped CSS

  • 1.11.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
111K
decreased by-22.46%
Maintainers
4
Weekly downloads
 
Created

What is @webcomponents/shadycss?

@webcomponents/shadycss is a library that provides a polyfill for the Shadow DOM v1 and Custom Elements v1 specifications, specifically focusing on the CSS scoping and encapsulation features. It allows developers to use Shadow DOM and Custom Elements with style encapsulation in browsers that do not fully support these features natively.

What are @webcomponents/shadycss's main functionalities?

Scoping CSS to Shadow DOM

This feature allows you to scope CSS styles to the Shadow DOM of a custom element, ensuring that styles do not leak out and affect other parts of the document.

import { styleElement } from '@webcomponents/shadycss';

class MyElement extends HTMLElement {
  constructor() {
    super();
    const shadow = this.attachShadow({ mode: 'open' });
    shadow.innerHTML = `
      <style>
        :host {
          display: block;
          color: red;
        }
      </style>
      <div>Content of my element</div>
    `;
    styleElement(this);
  }
}
customElements.define('my-element', MyElement);

Applying CSS Custom Properties

This feature allows you to use and apply CSS custom properties (variables) within the Shadow DOM, providing a way to define and reuse styles dynamically.

import { applyStyle } from '@webcomponents/shadycss';

class MyElement extends HTMLElement {
  constructor() {
    super();
    const shadow = this.attachShadow({ mode: 'open' });
    shadow.innerHTML = `
      <style>
        :host {
          --my-color: blue;
          color: var(--my-color);
        }
      </style>
      <div>Content of my element</div>
    `;
    applyStyle(this);
  }
}
customElements.define('my-element', MyElement);

Updating Styles Dynamically

This feature allows you to update the styles of a custom element dynamically by changing CSS custom properties and calling the updateStyles function to reapply the styles.

import { updateStyles } from '@webcomponents/shadycss';

class MyElement extends HTMLElement {
  constructor() {
    super();
    const shadow = this.attachShadow({ mode: 'open' });
    shadow.innerHTML = `
      <style>
        :host {
          --my-color: blue;
          color: var(--my-color);
        }
      </style>
      <div>Content of my element</div>
    `;
  }

  changeColor(newColor) {
    this.style.setProperty('--my-color', newColor);
    updateStyles();
  }
}
customElements.define('my-element', MyElement);

Other packages similar to @webcomponents/shadycss

Keywords

FAQs

Package last updated on 30 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc